package com.svmc.app.task.adapter;

import java.util.ArrayList;

import com.svmc.app.task.model.RingtoneModel;

import android.content.Context;
import android.database.Cursor;
import android.media.RingtoneManager;
import android.net.Uri;
import android.provider.MediaStore;

public class RingtoneAdapter {
	Context mContext;
	
	public RingtoneAdapter(Context mContext) {
		super();
		this.mContext = mContext;
	}

	public Uri [] getAllRingtoneSystem (){
		ArrayList<RingtoneModel> listRing = new ArrayList<RingtoneModel>();
		
		RingtoneManager ringtoneMgr = new RingtoneManager(mContext);
		ringtoneMgr.setType(RingtoneManager.TYPE_ALARM);
		Cursor alarmsCursor = ringtoneMgr.getCursor();
		
		int alarmsCount = alarmsCursor.getCount();
		
		if (alarmsCount == 0 && !alarmsCursor.moveToFirst()) {
		    return null ;
		}
		Uri[] alarms = new Uri[alarmsCount];
		while(!alarmsCursor.isAfterLast() && alarmsCursor.moveToNext()) {
		    int currentPosition = alarmsCursor.getPosition();
		   alarms[currentPosition] = ringtoneMgr.getRingtoneUri(currentPosition);
		   
		}
		alarmsCursor.close();
		return alarms;
		
	}
	public ArrayList<RingtoneModel> getAllAudioSDcard(){
		String[] STAR = { "*" };  
		ArrayList<RingtoneModel> listRing = new ArrayList<RingtoneModel>();
		Uri allaudiosong = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
		String audioselection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
		Cursor cursor = mContext.getContentResolver().query(allaudiosong, STAR, audioselection, null, null);
		if(cursor != null){
			if(cursor.moveToFirst()){
				do{
					RingtoneModel ringtone = new RingtoneModel();
					ringtone.setTitle(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));
					ringtone.setPath(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)));
					listRing.add(ringtone);
				}while (cursor.moveToNext());
			}
		}
		cursor.close();
		
		
		return listRing;
		
		
	}

}
